package minecraft.xiyu.exchantedbook.procedures;

import minecraft.xiyu.exchantedbook.init.ExchantedBookModEnchantments;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class SacrificeEnchantmentHandler {

	@SubscribeEvent
	public static void onEntityHurt(LivingHurtEvent event) {
		LivingEntity targetEntity = event.getEntityLiving();
		DamageSource damageSource = event.getSource();
		LivingEntity sourceEntity = (LivingEntity) damageSource.getEntity();

		if (sourceEntity != null && sourceEntity.getMainHandItem().getItem() instanceof SwordItem) {
			int sacrificeLevel = EnchantmentHelper.getItemEnchantmentLevel(ExchantedBookModEnchantments.SACRIFICE.get(), sourceEntity.getMainHandItem());

			if (sacrificeLevel > 0) {
				double damageDealt = event.getAmount();
				double healthToSacrifice = damageDealt / 3;
				double additionalDamage = healthToSacrifice * 0.9;


				sourceEntity.hurt(DamageSource.GENERIC, (float) healthToSacrifice);
				DamageSource sacrificeSource = new DamageSource("sacrifice");
				targetEntity.hurt(sacrificeSource, (float) additionalDamage);
			}
		}
	}
}
